﻿using System;
using System.Web.Mail;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MLY;
using MLY.Dal;
using MLY.Bll;
using System.IO;

namespace MLY.Reports.ChartReports
{
    public partial class DataFlowReport_Special : System.Web.UI.Page
    {
        private bool ChartInitialized = false;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // direction
                if (SessParam.Culture == "he")
                {
                    //cssLiteral.Text = @"<link type=""text/css"" rel=""stylesheet"" href=""/Style/Rtl_Stylesheet.css"" />";
                    DirectionPanel.Direction = ContentDirection.RightToLeft;
                }
                else
                {
                    //cssLiteral.Text = @"<link type=""text/css"" rel=""stylesheet"" href=""/Style/Ltr_Stylesheet.css"" />";
                    DirectionPanel.Direction = ContentDirection.LeftToRight;
                }
                if (SessParam.Culture == "he")
                {
                    DataFlowChart.Legends[0].Docking = System.Web.UI.DataVisualization.Charting.Docking.Left;
                    DataFlowChart.Titles[0].Alignment = System.Drawing.ContentAlignment.TopRight;
                }
                SetAreasVisibility(1);
            }
        }

        private void SetChartSeriesVisibility()
        {
            DataFlowChart.Series[0]["StackedGroupName"] = "group1";
            DataFlowChart.Series[1]["StackedGroupName"] = "group1";
            DataFlowChart.Series[2]["StackedGroupName"] = "group2";
            DataFlowChart.Series[3]["StackedGroupName"] = "group2";
            DataFlowChart.Series[4]["StackedGroupName"] = "group3";
            DataFlowChart.Series[5]["StackedGroupName"] = "group3";
            DataFlowChart.Series[6]["StackedGroupName"] = "group4";
            DataFlowChart.Series[7]["StackedGroupName"] = "group4";

            int NumPeriods = int.Parse(NumPeriodsHiddenField.Value);
            System.Web.UI.DataVisualization.Charting.Series serie;

            // Series 4 visibility and names
            if ((NumPeriods < 4) || (txtFromDate4.Text.Length == 0) || (txtToDate4.Text.Length == 0))
            {
                serie = DataFlowChart.Series[7];
                DataFlowChart.Series.Remove(serie);
                serie = DataFlowChart.Series[6];
                DataFlowChart.Series.Remove(serie);
            }
            else
            {
                DataFlowChart.Series[7].Name = "4: " + txtFromDate4.Text + " - " + txtToDate4.Text + "_Current" + Environment.NewLine + Environment.NewLine;
                DataFlowChart.Series[6].Name = "4: " + txtFromDate4.Text + " - " + txtToDate4.Text;
            }

            // Series 3 visibility and names
            if ((NumPeriods < 3) || (txtFromDate3.Text.Length == 0) || (txtToDate3.Text.Length == 0))
            {
                serie = DataFlowChart.Series[5];
                DataFlowChart.Series.Remove(serie);
                serie = DataFlowChart.Series[4];
                DataFlowChart.Series.Remove(serie);
            }
            else
            {
                DataFlowChart.Series[5].Name = "3: " + txtFromDate3.Text + " - " + txtToDate3.Text + "_Current" + Environment.NewLine + Environment.NewLine;
                DataFlowChart.Series[4].Name = "3: " + txtFromDate3.Text + " - " + txtToDate3.Text;
            }

            // Series 2 visibility and names
            if ((NumPeriods < 2) || (txtFromDate2.Text.Length == 0) || (txtToDate2.Text.Length == 0))
            {
                serie = DataFlowChart.Series[3];
                DataFlowChart.Series.Remove(serie);
                serie = DataFlowChart.Series[2];
                DataFlowChart.Series.Remove(serie);
            }
            else
            {
                DataFlowChart.Series[3].Name = "2: " + txtFromDate2.Text + " - " + txtToDate2.Text + "_Current" + Environment.NewLine + Environment.NewLine;
                DataFlowChart.Series[2].Name = "2: " + txtFromDate2.Text + " - " + txtToDate2.Text;
            }

            // Series 1 names
            if ((txtFromDate.Text.Length > 0) && (txtToDate.Text.Length > 0))
            {
                DataFlowChart.Series[1].Name = "1: " + txtFromDate.Text + " - " + txtToDate.Text + "_Current" + Environment.NewLine + Environment.NewLine;
                DataFlowChart.Series[0].Name = "1: " + txtFromDate.Text + " - " + txtToDate.Text;
            }

            if (StageRBL.SelectedValue == "5")
            {
                if (NumPeriods >= 4)
                {
                    serie = DataFlowChart.Series[7];
                    DataFlowChart.Series.Remove(serie);
                }
                if (NumPeriods >= 3)
                {
                    serie = DataFlowChart.Series[5];
                    DataFlowChart.Series.Remove(serie);
                }
                if (NumPeriods >= 2)
                {
                    serie = DataFlowChart.Series[3];
                    DataFlowChart.Series.Remove(serie);
                }
                serie = DataFlowChart.Series[1];
                DataFlowChart.Series.Remove(serie);
            }
        }

        private void FillInChartSerie(int SerieNum)
        {
            using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MLYConnectionString"].ConnectionString))
            {
                int index = 1;
                string stage = StageRBL.SelectedValue;
                string DateType = DateTypeDDL.SelectedValue;

                string StartDate = "";
                string EndDate = "";
                switch (SerieNum)
                {
                    case 0:
                        StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate.Text.Trim()));
                        EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate.Text.Trim()));
                        break;
                    case 1:
                        StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate.Text.Trim()));
                        EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate.Text.Trim()));
                        break;
                    case 2:
                        StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate2.Text.Trim()));
                        EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate2.Text.Trim()));
                        break;
                    case 3:
                        StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate2.Text.Trim()));
                        EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate2.Text.Trim()));
                        break;
                    case 4:
                        StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate3.Text.Trim()));
                        EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate3.Text.Trim()));
                        break;
                    case 5:
                        StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate3.Text.Trim()));
                        EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate3.Text.Trim()));
                        break;
                    case 6:
                        StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate4.Text.Trim()));
                        EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate4.Text.Trim()));
                        break;
                    case 7:
                        StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate4.Text.Trim()));
                        EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate4.Text.Trim()));
                        break;
                }

                SqlCommand cmd = new SqlCommand("Report_DataFlowReport_Special", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataReader reader;

                string Current;
                if ((SerieNum == 0) || (SerieNum == 2) || (SerieNum == 4) || (SerieNum == 6))
                    Current = "0";
                else
                    Current = "1";

                cmd.Parameters.AddWithValue("Stage", stage);
                cmd.Parameters.AddWithValue("DateType", DateType);
                cmd.Parameters.AddWithValue("StartDate", StartDate);
                cmd.Parameters.AddWithValue("EndDate", EndDate);
                cmd.Parameters.AddWithValue("Step", StepDDL.SelectedValue);
                cmd.Parameters.AddWithValue("MaxNum", MaxDaysDDL.SelectedValue);
                cmd.Parameters.AddWithValue("Current", Current);
                cmd.Parameters.AddWithValue("TextA", TextFieldABox.Text.Trim());
                cmd.Parameters.AddWithValue("TextC", TextFieldCBox.Text.Trim());
                try
                {
                    conn.Open();
                    reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            if (!ChartInitialized)
                            {
                                System.Web.UI.DataVisualization.Charting.CustomLabel CustomLabel =
                                    new System.Web.UI.DataVisualization.Charting.CustomLabel
                                {
                                    Text = reader.GetString(0),
                                    FromPosition = index - 0.5,
                                    ToPosition = index + 0.5
                                };
                                DataFlowChart.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(CustomLabel);
                            }
                            DataFlowChart.Series[SerieNum].Points.AddXY((double)reader.GetInt32(1), (double)reader.GetInt32(2));

                            index++;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteError("Alert mail send had failed with error", ex);
                }
                finally
                {
                    conn.Close();
                    ChartInitialized = true;
                }
            }
        }

        protected void GenerateReport(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                bool CurrentNeeded = (StageRBL.SelectedValue != "5");
                int NumPeriods = int.Parse(NumPeriodsHiddenField.Value);

                FillInChartSerie(0);
                if (CurrentNeeded)
                    FillInChartSerie(1);
                if ((NumPeriods >= 2) && (txtFromDate2.Text.Length > 0) && (txtToDate2.Text.Length > 0))
                {
                    FillInChartSerie(2);
                    if (CurrentNeeded)
                        FillInChartSerie(3);                    
                }
                if ((NumPeriods >= 3)&& (txtFromDate3.Text.Length > 0) && (txtToDate3.Text.Length > 0))
                {
                    FillInChartSerie(4);
                    if (CurrentNeeded)
                        FillInChartSerie(5);
                }
                if ((NumPeriods >= 4)&& (txtFromDate4.Text.Length > 0) && (txtToDate4.Text.Length > 0))
                {
                    FillInChartSerie(6);
                    if (CurrentNeeded)
                        FillInChartSerie(7);
                }

                SetChartSeriesVisibility();                
                
                //DBG.Text = DataFlowChart.Series.Count.ToString();
                //for (int i = 0; i < DataFlowChart.Series.Count; i++)
                //{
                //    DBG.Text += "<br /> " + DataFlowChart.Series[i].Name;
                //}

                //using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MLYConnectionString"].ConnectionString))
                //{
                //    int index = 1;
                //    string stage = StageRBL.SelectedValue;
                //    string DateType = DateTypeDDL.SelectedValue;

                //    string StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate.Text.Trim()));
                //    string EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate.Text.Trim()));

                //    SqlCommand cmd = new SqlCommand("Report_DataFlowReport_Special", conn);
                //    cmd.CommandType = CommandType.StoredProcedure;

                //    SqlDataReader reader;

                //    cmd.Parameters.AddWithValue("Stage", stage);
                //    cmd.Parameters.AddWithValue("DateType", DateType);
                //    cmd.Parameters.AddWithValue("StartDate", StartDate);
                //    cmd.Parameters.AddWithValue("EndDate", EndDate);
                //    cmd.Parameters.AddWithValue("Step", StepDDL.SelectedValue);
                //    cmd.Parameters.AddWithValue("MaxNum", MaxDaysDDL.SelectedValue);
                //    cmd.Parameters.AddWithValue("Current", "0");
                //    cmd.Parameters.AddWithValue("TextA", TextFieldABox.Text.Trim());
                //    cmd.Parameters.AddWithValue("TextC", TextFieldCBox.Text.Trim());

                //    try
                //    {
                //        conn.Open();
                //        reader = cmd.ExecuteReader();
                //        if (reader.HasRows)
                //        {

                //            while (reader.Read())
                //            {
                //                System.Web.UI.DataVisualization.Charting.CustomLabel CustomLabel =
                //                    new System.Web.UI.DataVisualization.Charting.CustomLabel
                //                    {
                //                        Text = reader.GetString(0),
                //                        FromPosition = index - 0.5,
                //                        ToPosition = index + 0.5
                //                    };
                //                DataFlowChart.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(CustomLabel);
                //                DataFlowChart.Series[0].Points.AddXY((double)reader.GetInt32(1), (double)reader.GetInt32(2));

                //                index++;
                //            }
                //        }
                //    }
                //    catch (Exception ex)
                //    {
                //        Log.WriteError("Alert mail send had failed with error", ex);
                //    }
                //    finally
                //    {
                //        conn.Close();
                //    }

                //    // "Currently in stage" is read only if the stage is not "from the beginning till the end".
                //    if (stage != "5")
                //    {

                //        // reading the "current" values
                //        index = 1;
                //        cmd.Parameters["Current"].Value = "1";

                //        try
                //        {
                //            conn.Open();
                //            reader = cmd.ExecuteReader();
                //            if (reader.HasRows)
                //            {

                //                while (reader.Read())
                //                {
                //                    DataFlowChart.Series[1].Points.AddXY((double)reader.GetInt32(1), (double)reader.GetInt32(2));
                //                    index++;
                //                }
                //            }
                //        }
                //        catch (Exception ex)
                //        {
                //            Log.WriteError("Graph creation failed with error", ex);
                //        }
                //        finally
                //        {
                //            conn.Close();
                //        }
                //    }
                //    else
                //    {
                //        System.Web.UI.DataVisualization.Charting.Series secondSerie = DataFlowChart.Series[1];
                //        DataFlowChart.Series.Remove(secondSerie);

                //    }

                //    if (txtFromDate2.Text.Trim().Length > 0 && txtToDate2.Text.Trim().Length > 0)
                //    {
                //        string StartDate2 = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate2.Text.Trim()));
                //        string EndDate2 = MLY.Utils.SqlDate(DateTime.Parse(txtToDate2.Text.Trim()));
                //        cmd.Parameters["StartDate"].Value = StartDate2;
                //        cmd.Parameters["EndDate"].Value = EndDate2;
                //        cmd.Parameters["Current"].Value = "0";

                //        try
                //        {
                //            conn.Open();
                //            reader = cmd.ExecuteReader();
                //            if (reader.HasRows)
                //            {

                //                while (reader.Read())
                //                {
                //                    DataFlowChart.Series[maxSerieNum - 1].Points.AddXY((double)reader.GetInt32(1), (double)reader.GetInt32(2));
                //                }
                //            }
                //        }
                //        catch (Exception ex)
                //        {
                //            Log.WriteError("Second part of the graph creation failed with error", ex);
                //        }
                //        finally
                //        {
                //            conn.Close();
                //        }

                //        if (stage != "5")
                //        {

                //            // reading the "current" values
                //            cmd.Parameters["Current"].Value = "1";

                //            try
                //            {
                //                conn.Open();
                //                reader = cmd.ExecuteReader();
                //                if (reader.HasRows)
                //                {

                //                    while (reader.Read())
                //                    {
                //                        DataFlowChart.Series[maxSerieNum].Points.AddXY((double)reader.GetInt32(1), (double)reader.GetInt32(2));
                //                    }
                //                }
                //            }
                //            catch (Exception ex)
                //            {
                //                Log.WriteError("Graph creation failed with error", ex);
                //            }
                //            finally
                //            {
                //                conn.Close();
                //            }
                //        }
                //        else
                //        {
                //            System.Web.UI.DataVisualization.Charting.Series secondSerie = DataFlowChart.Series[maxSerieNum];
                //            DataFlowChart.Series.Remove(secondSerie);
                //            maxSerieNum--;
                //        }

                //    }
                //    else
                //    {
                //        System.Web.UI.DataVisualization.Charting.Series secondSerie2 = DataFlowChart.Series[maxSerieNum];
                //        DataFlowChart.Series.Remove(secondSerie2);
                //        System.Web.UI.DataVisualization.Charting.Series secondSerie1 = DataFlowChart.Series[maxSerieNum - 1];
                //        DataFlowChart.Series.Remove(secondSerie1);
                //    }
                //}

                DataFlowChart.Visible = true;
            }
        }

        //protected void ShowSecondDatesSet(object sender, EventArgs e)
        //{
        //    Addition1.Visible = true;
        //    txtFromDate2.Text = "";
        //    DateTillTitleLabel2.Visible = true;
        //    Addition2.Visible = true;
        //    txtToDate2.Text = "";
        //    DateFromAndLabel.Visible = true;
        //    DateFromLabel2.Visible = true;

        //    OpenSecondDatesGroupButton.Visible = false;
        //    HideSecondDatesButton.Visible = true;

        //    Setters2Panel.Visible = true;
        //}

        //protected void HideSecondDates(object sender, EventArgs e)
        //{
        //    Addition1.Visible = false;
        //    txtFromDate2.Text = "";
        //    DateTillTitleLabel2.Visible = false;
        //    Addition2.Visible = false;
        //    txtToDate2.Text = "";
        //    DateFromAndLabel.Visible = false;
        //    DateFromLabel2.Visible = false;

        //    OpenSecondDatesGroupButton.Visible = true;
        //    HideSecondDatesButton.Visible = false;

        //    Setters2Panel.Visible = false;
        //}

        #region SetQY

        protected void SetQY1(object sebder, EventArgs e)
        {
            string q = QSetter1.SelectedValue;
            string y = YSetter1.SelectedValue;

            string startdate = "";
            string enddate = "";

            if (q != "0" && y != "0")
            {
                if (q == "1")
                {
                    startdate = "01/01/";
                    enddate = "31/03/";
                }
                else if (q == "2")
                {
                    startdate = "01/04/";
                    enddate = "30/06/";
                }
                else if (q == "3")
                {
                    startdate = "01/07/";
                    enddate = "30/09/";
                }
                else if (q == "4")
                {
                    startdate = "01/10/";
                    enddate = "31/12/";
                }

                startdate += y;
                enddate += y;

                txtFromDate.Text = startdate;
                txtToDate.Text = enddate;
                    
            }
        }

        protected void SetQY2(object sebder, EventArgs e)
        {
            string q = QSetter2.SelectedValue;
            string y = YSetter2.SelectedValue;

            string startdate = "";
            string enddate = "";

            if (q != "0" && y != "0")
            {
                if (q == "1")
                {
                    startdate = "01/01/";
                    enddate = "31/03/";
                }
                else if (q == "2")
                {
                    startdate = "01/04/";
                    enddate = "30/06/";
                }
                else if (q == "3")
                {
                    startdate = "01/07/";
                    enddate = "30/09/";
                }
                else if (q == "4")
                {
                    startdate = "01/10/";
                    enddate = "31/12/";
                }

                startdate += y;
                enddate += y;

                txtFromDate2.Text = startdate;
                txtToDate2.Text = enddate;

            }
        }

        protected void SetQY3(object sebder, EventArgs e)
        {
            string q = QSetter3.SelectedValue;
            string y = YSetter3.SelectedValue;

            string startdate = "";
            string enddate = "";

            if (q != "0" && y != "0")
            {
                if (q == "1")
                {
                    startdate = "01/01/";
                    enddate = "31/03/";
                }
                else if (q == "2")
                {
                    startdate = "01/04/";
                    enddate = "30/06/";
                }
                else if (q == "3")
                {
                    startdate = "01/07/";
                    enddate = "30/09/";
                }
                else if (q == "4")
                {
                    startdate = "01/10/";
                    enddate = "31/12/";
                }

                startdate += y;
                enddate += y;

                txtFromDate3.Text = startdate;
                txtToDate3.Text = enddate;

            }
        }


        protected void SetQY4(object sebder, EventArgs e)
        {
            string q = QSetter4.SelectedValue;
            string y = YSetter4.SelectedValue;

            string startdate = "";
            string enddate = "";

            if (q != "0" && y != "0")
            {
                if (q == "1")
                {
                    startdate = "01/01/";
                    enddate = "31/03/";
                }
                else if (q == "2")
                {
                    startdate = "01/04/";
                    enddate = "30/06/";
                }
                else if (q == "3")
                {
                    startdate = "01/07/";
                    enddate = "30/09/";
                }
                else if (q == "4")
                {
                    startdate = "01/10/";
                    enddate = "31/12/";
                }

                startdate += y;
                enddate += y;

                txtFromDate4.Text = startdate;
                txtToDate4.Text = enddate;
            }
        }
        #endregion SetQy

        protected void SetNumberPfPeriods(object sender, EventArgs e)
        {
            SetAreasVisibility(int.Parse(NumPeriodsDDL.SelectedValue));            
        }

        private void SetAreasVisibility(int NumPeriods)
        {
            NumPeriodsHiddenField.Value = NumPeriods.ToString(); 
            
            Set2Panel1.Visible = (NumPeriods >= 2);
            Set2Panel2.Visible = (NumPeriods >= 2);
            Set2Panel3.Visible = (NumPeriods >= 2);
            Set2Panel4.Visible = (NumPeriods >= 2);
            Set2Panel5.Visible = (NumPeriods >= 2);

            if (NumPeriods < 2)
            {
                txtFromDate2.Text = "";
                txtToDate2.Text = "";
                QSetter2.SelectedIndex = 0;
                YSetter2.SelectedIndex = 0;
            }

            Set3Panel1.Visible = (NumPeriods >= 3);
            Set3Panel2.Visible = (NumPeriods >= 3);
            Set3Panel3.Visible = (NumPeriods >= 3);
            Set3Panel4.Visible = (NumPeriods >= 3);
            Set3Panel5.Visible = (NumPeriods >= 3);

            if (NumPeriods < 3)
            {
                txtFromDate3.Text = "";
                txtToDate3.Text = ""; 
                QSetter3.SelectedIndex = 0;
                YSetter3.SelectedIndex = 0;
            }

            Set4Panel1.Visible = (NumPeriods >= 4);
            Set4Panel2.Visible = (NumPeriods >= 4);
            Set4Panel3.Visible = (NumPeriods >= 4);
            Set4Panel4.Visible = (NumPeriods >= 4);
            Set4Panel5.Visible = (NumPeriods >= 4);

            if (NumPeriods < 4)
            {
                txtFromDate4.Text = "";
                txtToDate4.Text = "";
                QSetter4.SelectedIndex = 0;
                YSetter4.SelectedIndex = 0;
            }
        }


        //private int CountWorkOrders(int currentMin, int currentMax, int maxInterval, List<WorkOrdersView> lst, int lifecycle)
        //{
        //    int Count = 0;

        //    DateTime CurDate = DateTime.Now;

        //    //string FromDate = FromDateDDL.SelectedValue;
        //    //string TillDate = TillDateDDL.SelectedValue;
        //    /*
        //      SendDate 1
        //      DateFieldB 2
        //      PlannedDelivaryDate 3                      
        //      ActualDelivaryDate 4
        //     */
        //    if (FromDate == TillDate)
        //        Count = 0;
        //    else if (FromDate == "1" && TillDate == "2")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate) -
        //                                                                       (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate) -
        //                                                                       (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    else if (FromDate == "1" && TillDate == "3")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
        //                                                                       (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
        //                                                                       (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    else if (FromDate == "1" && TillDate == "4")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate) -
        //                                                                       (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate) -
        //                                                                       (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    else if (FromDate == "2" && TillDate == "1")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
        //                                                                       (l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
        //                                                                       (l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    else if (FromDate == "2" && TillDate == "3")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
        //                                                                       (l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
        //                                                                       (l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    else if (FromDate == "2" && TillDate == "4")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate) -
        //                                                                       (l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate) -
        //                                                                       (l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    else if (FromDate == "3" && TillDate == "1")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
        //                                                                       (l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
        //                                                                       (l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    else if (FromDate == "3" && TillDate == "2")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate) -
        //                                                                       (l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate) -
        //                                                                       (l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    else if (FromDate == "3" && TillDate == "4")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate) -
        //                                                                       (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate) -
        //                                                                       (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    else if (FromDate == "4" && TillDate == "1")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
        //                                                                       (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
        //                                                                       (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
        //    }

        //    else if (FromDate == "4" && TillDate == "2")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate) -
        //                                                                       (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.DateFieldB.HasValue ? l.DateFieldB.Value : CurDate) -
        //                                                                       (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    else if (FromDate == "4" && TillDate == "3")
        //    {
        //        Count = lst.Where(l => l.CurrentLifeCycleStage.HasValue && l.CurrentLifeCycleStage.Value == lifecycle &&
        //                         (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
        //                                                                       (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
        //                              ((currentMax > maxInterval) ||
        //                               (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
        //                                                                       (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
        //    }
        //    return Count;
        //}
    }
}
